package com.gdip.controller;


import com.gdip.common.dtos.ResponseResult;
import com.gdip.dto.UserDTO;
import com.gdip.po.UserPo;
import com.gdip.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/register")
    public ResponseResult register(@RequestBody UserDTO userDTO){
        System.out.println(userDTO);
        return userService.register(userDTO);
    }


    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserDTO userDTO, HttpSession session){
        System.out.println(session.getId());
        return userService.login(userDTO,session);
    }

    @GetMapping("/getUserData")
    public ResponseResult getUserData(HttpSession session){
        Integer userId = (Integer) session.getAttribute("userId");
        System.out.println("用户id"+session.getId());

        return userService.getUserData(userId);
    }

    @GetMapping("/hello")
    public void test(HttpSession session){
        System.out.println("hello"+session.getAttribute("userId"));
    }

    // 管理员操作-按账号模糊查询批量用户
    @GetMapping("/getUserByLike")
    public ResponseResult getUserByLike(@RequestParam String username) {
        return userService.getUserByLike(username);
    }

    // 管理员操作-批量设置用户权限
    @PostMapping("/set_permissions")
    public ResponseResult set_permissions(@RequestBody List<UserPo> list) {
        return userService.set_permissions(list);
    }

    @GetMapping("/getSchoolClass")
    public ResponseResult getSchoolClass(@RequestParam int id) {
        return userService.getSchoolClassById(id);
    }

    @GetMapping("/getRoles")
    public ResponseResult getRoles(){
        return userService.getRoles();
    }

    @GetMapping("/searchRoles")
    public ResponseResult searchRoles(@RequestParam("username") String username,@RequestParam String role){
        return userService.searchRoles(username,role);
    }

    @PostMapping("/updateRole")
    public ResponseResult updateRole(@RequestBody UserDTO userDTO){
        return userService.updateRole(userDTO);
    }

}
